package com.ruoyi.business.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.business.domain.dto.SkTokenGroupDTO;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableSupport;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.SkToken;
import com.ruoyi.business.service.ISkTokenService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 令牌余量Controller
 * 
 * @author ruoyi
 * @date 2025-03-06
 */
@RestController
@RequestMapping("/business/sk-token")
public class SkTokenController extends BaseController
{
    @Autowired
    private ISkTokenService skTokenService;

    /**
     * 查询令牌余量列表
     */
    @PreAuthorize("@ss.hasPermi('business:sk-token:list')")
    @GetMapping("/list")
    public TableDataInfo list(SkToken skToken)
    {
        startPage();
        List<SkToken> list = skTokenService.selectSkTokenList(skToken);
        return getDataTable(list);
    }

    /**
     * 导出令牌余量列表
     */
    @PreAuthorize("@ss.hasPermi('business:sk-token:export')")
    @Log(title = "令牌余量", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SkToken skToken)
    {
        List<SkToken> list = skTokenService.selectSkTokenList(skToken);
        ExcelUtil<SkToken> util = new ExcelUtil<SkToken>(SkToken.class);
        util.exportExcel(response, list, "令牌余量数据");
    }

    /**
     * 获取令牌余量详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:sk-token:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(skTokenService.selectSkTokenById(id));
    }

    /**
     * 新增令牌余量
     */
    @PreAuthorize("@ss.hasPermi('business:sk-token:add')")
    @Log(title = "令牌余量", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SkToken skToken)
    {
        return toAjax(skTokenService.insertSkToken(skToken));
    }

    /**
     * 修改令牌余量
     */
    @PreAuthorize("@ss.hasPermi('business:sk-token:edit')")
    @Log(title = "令牌余量", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SkToken skToken)
    {
        return toAjax(skTokenService.updateSkToken(skToken));
    }

    /**
     * 删除令牌余量
     */
    @PreAuthorize("@ss.hasPermi('business:sk-token:remove')")
    @Log(title = "令牌余量", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(skTokenService.deleteSkTokenByIds(ids));
    }


    /**
     * 按车次分组查询令牌余量列表
     */
    @PreAuthorize("@ss.hasPermi('business:sk-token:list')")
    @GetMapping("/listByGroup")
    public TableDataInfo listByGroup(SkToken skToken) {
        // 获取分组前的所有数据
        List<SkTokenGroupDTO> groups = skTokenService.selectSkTokenGroupByTrainCode(skToken);

        // 手动分页
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();

        // 计算总数和当前页数据
        int total = groups.size();
        int fromIndex = (pageNum - 1) * pageSize;
        int toIndex = Math.min(fromIndex + pageSize, total);

        List<SkTokenGroupDTO> pageData;
        if (fromIndex < total) {
            pageData = groups.subList(fromIndex, toIndex);
        } else {
            pageData = new ArrayList<>();
        }

        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(pageData);
        rspData.setTotal(total);
        return rspData;
    }
}
